<%@ taglib prefix="s" uri="/struts-tags" %>
<%@page  pageEncoding="UTF-8" %>
<script type="text/javascript">
	$(document).ready(function() {
		$('tr').each(function() {
			if ($(this).attr('errorfor') != null) {
				$(this).empty();
			}
		});
	});
</script>
<h2>
	<s:if test="id == null">
		Add new "<s:property value="%{allSubjectTypes[subjectType - 1].typeName}" />"
	</s:if>
	<s:else>Edit "<s:property value="%{allSubjectTypes[subjectType - 1].typeName}" />" entry</s:else>
</h2>
<s:form action="%{action}">
	
	<s:hidden name="id" value="%{id}" />
	<s:hidden name="subjectType" value="%{subjectType}" />
	<s:hidden name="action" value="%{action}" />
	<s:hidden name="personId" value="%{personId}" />
	<s:hidden name="enterpriseId" value="%{enterpriseId}" />
	<s:hidden name="employeeId" value="%{employeeId}" />

	<s:textfield label="Firstname" name="firstName" />
	<s:fielderror fieldName="firstname"/>
	
	<s:if test="subjectType!=@ee.siimkaspar.subjects.controller.EditSubjectAction@ENTERPRISE">
		<s:textfield label="Lastname" name="lastName" />
		<td><s:fielderror fieldName="lastname"/></td>
	
		<s:textfield label="Birth date" name="birthDate" /></td>
		<td><s:fielderror fieldName="birthDate"/>
		
		<s:textfield label="Social security number" name="ssn" />
		<td><s:fielderror fieldName="ssn"/></td>
	</s:if>
	<s:else>
		<s:textfield label="Fullname" name="lastName" />
		<s:fielderror fieldName="lastname"/>
	</s:else>
	<s:if test="%{action == @ee.siimkaspar.subjects.controller.EditSubjectAction@ACTION_EDIT}">
		<s:label value="--------------------------" />
		<s:if test="%{customerIs}">
			<s:checkbox label="Is customer?" name="customerIs" labelposition="left" disabled="true" />
		</s:if>
		<s:else>
			<s:checkbox label="Is customer?" name="customerIs" labelposition="left" />
			<td><s:fielderror fieldName="customerIs"/></td>
		</s:else>
	</s:if>
	
	<s:label value="--------------------------" />
	<s:iterator value="contacts" status="stat" var="contact">
         <s:textfield label="%{#contact.contactType.typeName}" value="%{#contact.valueText}" name="contacts[%{#stat.count}].valueText"/>
    	 <s:hidden name="contacts[%{#stat.count}].id" value="%{#contact.id}"/>
    </s:iterator>
	
	<s:if test="subjectType == @ee.siimkaspar.subjects.controller.EditSubjectAction@ENTERPRISE">
		<s:label value="----- Legal address -----" />
	</s:if>
	<s:else>
		<s:label value="----- Main aadress -----" />
	</s:else>
	<s:hidden name="address.id" value="%{address.id}"/>
	
	<s:textfield label="Country" name="address.country"/>
	<td><s:fielderror fieldName="address.country"/></td>
	
	<s:textfield label="County" name="address.county"/>
	<td><s:fielderror fieldName="address.county"/></td>
	
	<s:textfield label="Town/village" name="address.townVillage"/>
	<td><s:fielderror fieldName="address.county"/></td>
	
	<s:textfield label="StreetAddress" name="address.streetAddress"/>
	<td><s:fielderror fieldName="address.streetAddress"/></td>
	
	<s:textfield label="Zipcode" name="address.zipcode"/>
	<td><s:fielderror fieldName="address.zipcode"/></td>
	
	<s:set var="cnt" value="0" />
	<s:iterator value="attributes" status="stat">
		<s:label value="---- \"%{key}\" data ----" />
		<s:iterator value="value" id="attribute" status="valStat">
			<s:if test="#attribute.dataType == @ee.siimkaspar.subjects.util.Const@DATA_TYPE_DATE">
				<s:textfield label="%{#attribute.subjectAttributeType.typeName}" value="%{#attribute.valueDate}" name="attributesResultList[%{#cnt}]" />
			</s:if>
			<s:elseif test="#attribute.dataType == @ee.siimkaspar.subjects.util.Const@DATA_TYPE_NUMBER">
				<s:textfield label="%{#attribute.subjectAttributeType.typeName}" value="%{#attribute.valueNumber}" name="attributesResultList[%{#cnt}]" />
			</s:elseif>
			<s:else>
				<s:textfield label="%{#attribute.subjectAttributeType.typeName}" value="%{#attribute.valueText}" name="attributesResultList[%{#cnt}]" />
			</s:else>
			<s:set var="cnt" value="%{#cnt + 1}" />
		</s:iterator>
		
	</s:iterator>
	<s:submit value="Save"/>
	<s:if test="%{action == @ee.siimkaspar.subjects.controller.EditSubjectAction@ACTION_EDIT}">
		<s:submit action="deleteSubject" value="Delete"/>
	</s:if>
</s:form>
<s:form action="">
	<s:submit value="Change addresses" />
</s:form>
<s:form action="">
	<s:submit value="Change contacts" />
</s:form>
<s:if test="subjectType == @ee.siimkaspar.subjects.controller.EditSubjectAction@ENTERPRISE">
	<s:form action="">
		<s:submit value="Add employees"/>
	</s:form>
</s:if>